<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>模型管理</title>
    <link rel="stylesheet" href="__ADMIN__/css/amazeui.min.css" />
    <link rel="stylesheet" href="__ADMIN__/css/app.css">
    <link rel="stylesheet" href="__STATIC__/layui/css/layui.css">
    <script src="__COMMON__/jquery.min.js"></script>
</head>
<body>
<div style="background: #e9ecf3" class="theme-white">
    <div class="row-content am-cf">
        <div class="row">
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-12" style="background: white">
                <div class="widget am-cf">
                    <div data-am-widget="titlebar" class="am-titlebar am-titlebar-default">
                        <h2 class="am-titlebar-title">模型管理</h2>
                    </div>
                    <div class="widget-body  am-fr">

                        <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                            <div class="am-form-group">
                                <div class="am-btn-toolbar">
                                    <div class="am-btn-group am-btn-group-xs">
                                        <button type="button" class="am-btn am-btn-default am-btn-success" id="add">
                                            <span class="am-icon-plus"></span> 新增
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
                            <div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
                                <input type="text" class="am-form-field " id="searchText" placeholder="模型名称">
                                <span class="am-input-group-btn" id="search-btn">
                                    <button class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search" type="button"></button>
                                </span>
                            </div>
                        </div>

                        <div class="am-u-sm-12">
                            <table class="layui-hide" id="data-table" lay-filter="data-table"></table>

                            <script type="text/html" id="toolbar">

                            </script>

                            <script type="text/html" id="content-bar">
                                <div class="tpl-table-black-operation">
                                    <a href="javascript:;" lay-event="content"><i class="am-icon-cog"></i> 内容字段</a>
                                </div>
                            </script>

                            <script type="text/html" id="opt-bar">
                                <div class="tpl-table-black-operation">
                                    <a href="javascript:;" lay-event="edit"><i class="am-icon-pencil"></i> 编辑</a>
                                    <a href="javascript:;" class="tpl-table-black-operation-del" lay-event="del"><i class="am-icon-trash"></i> 删除</a>
                                </div>
                            </script>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="__ADMIN__/js/amazeui.min.js"></script>
<script src="__STATIC__/layui/layui.js"></script>
<script>
    initTable();

    function initTable() {

        layui.use('table', function(){
            var table = layui.table;

            table.render({
                elem: '#data-table'
                ,url:'{:url("modelx/index")}'
                ,where: {model_name: $("#searchText").val()}
                ,toolbar: '#toolbar'
                ,defaultToolbar: ['filter', 'exports', 'print']
                ,title: '模型列表'
                ,cols: [[
                    {field:'model_id', title:'模型ID', width: 80}
                    ,{field:'name', title:'模型名称'}
                    ,{field:'table', title:'表名'}
                    ,{field:'channel_tpl', title:'栏目页模板'}
                    ,{field:'list_tpl', title:'列表页模板'}
                    ,{field:'show_tpl', title:'详情页模板'}
                    ,{field:'content', title:'内容字段', toolbar: '#content-bar'}
                    ,{fixed: 'right', title:'操作', toolbar: '#opt-bar', width:150}
                ]]
                ,page: {
                    theme: '#23abf0'
                }
            });

            table.on("tool(data-table)", function(e) {

                if ("edit" === e.event) {

                    layer.open({
                        type: 2,
                        title: '编辑模型',
                        maxmin: true,
                        shade: 0.2,
                        area: ['60%', '90%'],
                        content: "edit/id/" + e.data.model_id
                    });
                } else if ("del" == e.event) {

                    layer.confirm('您确定要删除该模型？删除后模型下的文章将全部被删除！', {
                        icon: 3,
                        title: '友情提示',
                        btn: ['确定', '取消']
                    }, function() {

                        $.getJSON('{:url("modelx/del")}', {id: e.data.model_id}, function (res) {
                            if (res.code == 0) {
                                layer.msg(res.msg);

                                setTimeout(function () {
                                    initTable();
                                }, 800);
                            } else {
                                layer.msg(res.msg);
                            }
                        });
                    }, function() {

                    });
                } else if ('content' == e.event) {
                    var url = '{:url("modelField/showField")}';
                    window.location.href = url + '?model_id=' + e.data.model_id;
                }
            });
        });
    }

    $(function () {

        layui.use('layer', function () {

            var layer = layui.layer;
            layer.ready(function () {

                $("#add").click(function () {

                    layer.open({
                        type: 2,
                        title: '添加模型',
                        maxmin: true,
                        shade: 0.2,
                        area: ['60%', '90%'],
                        content: '{:url("modelx/add")}'
                    });
                });
            });
        });
        
        // 查询
        $('#search-btn').click(function () {
            initTable();
        });
    });
</script>
</body>
</html>